<template>
        <el-card shadow="never" style="max-height: 700px; overflow: auto">
            <el-form ref="form" :model="form" :rules="rules" label-width="90px" :disabled="disabled">
                <el-form-item label="id:" prop="id" v-show="false">
                    <el-input v-model="form.id" disabled />
                </el-form-item>

                <el-form-item label="物流单号:" prop="tags">
                    <el-tag
                        :key="tag"
                        v-for="tag in tags"
                        closable
                        type=""
                        effect="plain"
                        :disable-transitions="false"
                        @close="handleClose(tag)">
                        {{tag}}
                    </el-tag>
                    <el-input
                        class="input-new-tag"
                        v-if="inputVisible"
                        v-model="inputValue"
                        ref="saveTagInput"
                        @keyup.enter.native="handleInputConfirm"
                        @blur="handleInputConfirm"
                        >
                    </el-input>
                    <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 物流单号</el-button>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="submitForm">确认 </el-button>
                    <el-button type="warning" @click="cancel">取消 </el-button>
                </el-form-item>
            </el-form>
        </el-card>
</template>

<script>
export default {
    name: 'EditForm_logistics',
    props: {
        findFormData: {
            type: Object,
            default: {}
        },
        editFormParams: {
            type: Object,
            default: {}
        },
        // 表单是否禁用
        disabled: {
            type: Boolean,
            default: false,
        },
        
    },
    data() {
        return {
            // 编辑表单验证规则
            rules: {
            },
            form: {  },
            purchase_data: [],
            colors: [],
            tags: [],
            inputVisible: false,
            inputValue: '',
            list: [],
            purchase: [],
            sums_count: 0,
            color_i: 0,
            id: 0,
            audit_list:[],
            fullscreenLoading: false,
            exportLoading: false,
            // 接口地址
            api: {
                upload: `https://api.fan-zhou.com/admin/awaiting/uploads`,
            },
        }
    },
    watch: {
        findFormData: {
            handler(nval) {
                if (JSON.stringify(nval) != '{}') {
                    if(nval.id > 0){
                        // this.edit_purchase_apply(nval.id)
                    }
                    this.$nextTick(() => {
                        this.purchase_data = nval.purchase_data
                        this.form = nval
                        this.id = nval.id
                        this.tags = nval.wl_num
                    })
                }
            },
            immediate: true,
            deep: false,
        },
        
    },
    methods: {
        ok() {
            this.$refs.mytb.setCurrentRow();
        },
        blurs() {
            // this.getSummaries();
            // this.$refs.mytb.setCurrentRow();
        },
        getSummaries(param) {
            const { columns, data } = param;
            const sums = [];
            columns.forEach((column, index) => {
                if (index === 0) {
                    sums[index] = '总计';
                    return;
                }
                if (index === 1) {
                    const values = data.map(item => Number(item['real_num']));
                    if (!values.every(value => isNaN(value))) {
                        sums[index] = values.reduce((prev, curr) => {
                        const value = Number(curr);
                        
                        if (!isNaN(value)) {
                            return prev + curr;
                        } else {
                            return prev;
                        }
                        }, 0);
                        sums[index] += '';
                        this.form.real_num = sums[index];
                    } else {
                        sums[index] = '未知';
                    }
                    return;
                }
                if (index === 2) {
                    const values = data.map(item => Number(item['real_num_correct']));
                    if (!values.every(value => isNaN(value))) {
                        sums[index] = values.reduce((prev, curr) => {
                        const value = Number(curr);
                        
                        if (!isNaN(value)) {
                            return prev + curr;
                        } else {
                            return prev;
                        }
                        }, 0);
                        sums[index] += '';
                        this.form.real_num_correct = sums[index];
                    } else {
                        sums[index] = '未知';
                    }
                    return;
                }
            });
            return sums;
        },
        // getSummaries(param) {
        //     const { columns, data } = param;
        //     const sums = [];
        //     const sums2 = [];
        //     let length2 = 0;
        //     if(this.list[0] !== undefined){
        //         length2 = this.list[0].length;
        //         length2 = length2-1;
        //     }
        //     if(length2 !== 0){
        //         sums[length2] = 0;
        //         for(let i =0;i<data.length;i++){
        //             for(let v =0;v<data[i].length-1;v++){
        //                 if(data[i] !== undefined){
        //                     if(v == 0){
        //                         sums[v] = '统计';
        //                     }else{
        //                         if(sums[v] == undefined){
        //                             if(!isNaN(Number(data[i][v]))){
        //                                 sums[v] = Number(data[i][v]);
        //                                 sums[length2] = Number(sums[length2]) + Number(data[i][v]) 
        //                             }else{
        //                                 sums[v] = 0;
        //                                 sums[length2] = 0;
        //                             }
        //                         }else{
        //                             if(!isNaN(Number(data[i][v]))){
        //                                 sums[v] = Number(sums[v])  + Number(data[i][v]);
        //                                 sums[length2] = Number(sums[length2]) + Number(data[i][v])
        //                             }else{
        //                                 sums[v] = Number(sums[v]);
        //                                 sums[length2] = Number(sums[v]);
        //                             }
        //                         }
        //                     }
        //                     if(sums2[i] == undefined){
        //                         if(!isNaN(Number(data[i][v]))){
        //                             sums2[i] = Number(data[i][v]);
        //                         }else{
        //                             sums2[i] = 0;
        //                         }
        //                     }else{
        //                         if(!isNaN(Number(data[i][v]))){
        //                             sums2[i] = Number(sums2[i])  + Number(data[i][v]);
        //                         }else{
        //                             sums2[i] = Number(sums2[i]);
        //                         }
        //                     }

        //                 }
        //             }
        //         }
        //         for(let i =0;i<data.length;i++){
        //             if(!isNaN(Number(sums2[i]))){
        //                 this.list[i][length2] = Number(sums2[i]);
        //             }
        //         }
        //     }
        //     this.sums_count = sums[length2];
        //     sums[0] = '统计('+this.sums_count+')';
        //     return sums;
        // },
        edit_purchase_apply(id){
                this.request
                    .post('/admin/output/edit_purchase_apply', { 
                        id:id,
                    })
                    .then(response => {
                        let new_purchase = [];
                        let new_list = [];
                        for(let i =0;i<response.table_header.length;i++){
                            if(response.table_header[i] == 'batch'){
                                // this.batchx = i;
                                new_purchase[i] =  '批次号'
                            }else if(response.table_header[i] == 'status'){
                                new_purchase[i] =  '审核状态'
                            }else if(response.table_header[i] == 'admin_id'){
                                new_purchase[i] =  '申请人'
                            }else if(response.table_header[i] == 'color'){
                                this.color_i = i;
                                new_purchase[i] =  '颜色'
                            }else if(response.table_header[i] == 'create_time'){
                                this.create_timex = i;
                                new_purchase[i] =  'create_time'
                            }else if(response.table_header[i] == 'color_sum'){
                                this.create_timex = i;
                                new_purchase[i] =  '统计'
                            }else{
                                new_purchase[i] =  response.table_header[i]
                            }
                        }
                        this.colors = response.options.color_list
                        this.audit_list = response.options.audit_list
                        for(let ic =0;ic<response.list.length;ic++){
                            response.list[ic][this.color_i] = this.transformation_color2(response.list[ic][this.color_i]);
                        }
                        this.list = response.list
                        this.purchase = new_purchase
                    })
                    .finally(() => {
                    })
        },
        // 取消按钮
        cancel() {
            this.$emit('editFormCancel')
        },

        submitForm() {
            this.$refs['form'].validate((valid, obj) => {
                if (valid) {
                    this.form.purchase_data = this.purchase_data;
                    this.$emit('submitForm', this.form)
                }
            })
        },

        // 表单重置
        reset() {
            this.resetForm('form')
        },

        
        // 导出按钮操作
        handleExport() {
            const qyparams = {
                id: this.id,
            }
            this.$confirm('是否确认导出数据?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    this.exportLoading = true
                    return this.request.get('/admin/awaiting/export_number', { params: qyparams, responseType: 'blob' })
                })
                .then(response => {
                    this.download(response,'待到货.xls')
                })
                .catch(err => {})
                .finally(() => {
                    this.exportLoading = false
                })
        },
        handlePreview(file) {
            console.log(file);
        },
        
        //上传
        submitUpload() {
            this.loading = true
            this.$refs.upload.submit();
        },
        // 上传成功回调
        handleUploadSuccess(res) {
            this.loading = false
            if (res.code == 1) {
                this.$message.success('上传成功');
                this.cancel();
            } else {
                this.$message({
                    type: 'error',
                    message: '上传失败'+res.msg,
                })
            }
        },
        // 上传失败
        handleUploadError() {
            this.$message({
                type: 'error',
                message: '上传失败',
            })
        },

        showInput() {
        this.inputVisible = true;
        this.$nextTick(_ => {
            this.$refs.saveTagInput.$refs.input.focus();
            });
        },
        handleInputConfirm() {
        let inputValue = this.inputValue;
        if (inputValue) {
            if(this.tags == ''){
                this.tags = []
            }
            this.tags.push(inputValue);
            this.form.wl_num = this.tags
            }
            this.inputVisible = false;
            this.inputValue = '';
        },
        handleClose(tag) {
            this.tags.splice(this.tags.indexOf(tag), 1);
            this.form.tags = this.tags
        },
    }
}
</script>
